#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @Time: 2020/3/17  14:04
# @Author: 余浪人
# @email: yulangren520@gmail.com

from wtforms import Form, StringField, validators, PasswordField,RadioField,TextAreaField
from apps.models.user_model import TL_User_Model


class TL_User_Form(Form):
    username = StringField(
        '登录名称', validators=[validators.DataRequired('请填写登录名称!'), validators.Length(2, message='登录名称长度不足!')],
        render_kw={"required": "", "class": "layui-input", "style": "width: 40%;",'placeholder': '输入登录名'})
    password = PasswordField(label=u'用户密码', validators=[validators.Length(min=6, message='密码至少6位!')],
        render_kw={"required": "", "class": "layui-input", "style": "width: 40%;",'placeholder': '输入登录密码'})
    password2 = PasswordField(
        label=u'确认密码', validators=[validators.EqualTo('password', '两次密码不一致!')],
        render_kw={"required": "", "class": "layui-input", "style": "width: 40%;",'placeholder': '输入重复登录密码'})
    user_email = StringField('用户邮箱', validators=[validators.DataRequired('请填写邮箱地址！'),validators.Email('邮箱地址格式不正确！')], render_kw={"class": "layui-input", "style": "width: 40%;",'placeholder': '输入电子邮箱地址'})
    user_integral = StringField('用户积分', validators=[validators.DataRequired('用户积分不可为空')],default=0, render_kw={"class": "layui-input", "style": "width: 40%;","oninput": "this.value=this.value.replace(/[^0-9\.]/g,'')"})

    user_qq = StringField('用户QQ', validators=[validators.Optional(True),validators.Length(max=20,message='QQ字符过长！')], render_kw={"class": "layui-input", "style": "width: 40%;","oninput": "this.value=this.value.replace(/[^0-9\.]/g,'')",'placeholder': '输入QQ号'})
    user_nickname = StringField('用户昵称', validators=[validators.Optional(True),validators.Length(max=64,message='昵称字符过长！')], render_kw={"class": "layui-input", "style": "width: 40%;",'placeholder': '输入用户昵称'})
    user_phone = StringField('用户电话', validators=[validators.Optional(True),validators.Length(11,11,'手机号码格式错误！'),validators.Regexp('^1[35789]d{9}$', 0, '手机号码不合法')], render_kw={"class": "layui-input", "style": "width: 40%;",'placeholder': '输入手机号'})
    user_gender = RadioField('用户性别', validators=[validators.DataRequired('选择性别')],choices=[(3,'保密'),(1,'男'),(2,'女')],coerce=int, default=3)
    remark = TextAreaField('备注信息', validators=[validators.Optional(True), validators.Length(max=512, message='备注信息过长!')], render_kw={"class": "layui-textarea", "style": "width: 60%;",'placeholder': '输入其他信息'})

    def validate_username(self, username):
        cou = TL_User_Model.query.filter(TL_User_Model.username == username.data)
        if cou.count(): raise validators.ValidationError("此登录名已存在!")

    def validate_user_email(self, user_email):
        cou = TL_User_Model.query.filter(TL_User_Model.user_email == user_email.data)
        if cou.count(): raise validators.ValidationError("此邮箱已存在!")

class TL_User_Edit_Form(Form):
    username = StringField(
        '登录名称', validators=[validators.DataRequired('请填写登录名称!'), validators.Length(2, message='登录名称长度不足!')],
        render_kw={"required": "", "class": "layui-input layui-disabled", "style": "width: 40%;",'placeholder': '输入登录名',"readonly": "readonly"})
    password = PasswordField(label=u'用户密码', validators=[validators.Optional(True),validators.Length(min=6, message='密码至少6位!')],
        render_kw={"required": "", "class": "layui-input", "style": "width: 40%;",'placeholder': '输入登录密码'})
    password2 = PasswordField(
        label=u'确认密码', validators=[validators.EqualTo('password', '两次密码不一致!')],
        render_kw={"required": "", "class": "layui-input", "style": "width: 40%;",'placeholder': '输入重复登录密码'})
    user_email = StringField('用户邮箱', validators=[validators.DataRequired('请填写邮箱地址！'),validators.Email('邮箱地址格式不正确！')], render_kw={"class": "layui-input", "style": "width: 40%;",'placeholder': '输入电子邮箱地址'})
    user_integral = StringField('用户积分', validators=[validators.DataRequired('用户积分不可为空')],default=0, render_kw={"class": "layui-input", "style": "width: 40%;","oninput": "this.value=this.value.replace(/[^0-9\.]/g,'')"})

    user_qq = StringField('用户QQ', validators=[validators.Optional(True),validators.Length(max=20,message='QQ字符过长！')], render_kw={"class": "layui-input", "style": "width: 40%;","oninput": "this.value=this.value.replace(/[^0-9\.]/g,'')",'placeholder': '输入QQ号'})
    user_nickname = StringField('用户昵称', validators=[validators.Optional(True),validators.Length(max=64,message='昵称字符过长！')], render_kw={"class": "layui-input", "style": "width: 40%;",'placeholder': '输入用户昵称'})
    user_phone = StringField('用户电话', validators=[validators.Optional(True),validators.Length(11,11,'手机号码格式错误！'),validators.Regexp('^1[35789]d{9}$', 0, '手机号码不合法')], render_kw={"class": "layui-input", "style": "width: 40%;",'placeholder': '输入手机号'})
    user_gender = RadioField('用户性别', validators=[validators.DataRequired('选择性别')],choices=[(3,'保密'),(1,'男'),(2,'女')],coerce=int, default=3)
    remark = TextAreaField('备注信息', validators=[validators.Optional(True), validators.Length(max=512, message='备注信息过长!')], render_kw={"class": "layui-textarea", "style": "width: 60%;",'placeholder': '输入其他信息'})


class TL_Grade_Form(Form):
    grade_name = StringField('等级名称',validators=[validators.DataRequired('等级名称不可为空')])
    grade_integral = StringField('等级积分',validators=[validators.DataRequired('等级积分不可为空')])
    def validate_grade_integral(self, grade_integral):
        try:
            int(grade_integral.data)
        except:
            raise validators.ValidationError("积分不是一个整数!")